package studentSystem;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void startStudentSystem(){
        ArrayList<Student> list = new ArrayList<>();
        loop:while (true) {
            System.out.println("-------------------欢迎来到学生管理系统-----------------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    searchStudent(list);
                    break;
                case "5": {
                    System.out.println("退出系统，欢迎下次使用！");
//                    break loop;
                    System.exit(0);
                }
                default:
                    System.out.println("没有该选项，请重新输入");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list){
        System.out.println("添加学生");
        Scanner sc = new Scanner(System.in);

        String id;
        while (true) {
            System.out.println("请输入学生学号：");
             id = sc.next();
            boolean flag = contains(list, id);
            if(flag){
                System.out.println("id已存在，请重新输入！");
            } else {
                break;
            }
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();

        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();

        System.out.println("请输入学生地址：");
        String address = sc.next();

        Student stu = new Student(id, name, age, address);
        list.add(stu);
        System.out.println("学号为"+stu.getId()+"的学生添加成功！");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        System.out.println("删除学生");
        System.out.println("请输入要删除学生的学号：");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int pos = index(list, id);
        if(pos >= 0){
            list.remove(pos);
            System.out.println("学号为"+id+"的学生删除成功！");
        } else{
            System.out.println("id不存在，删除失败！");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的学号：");
        String id = sc.next();
        int pos = index(list, id);
        if(pos == -1){
            System.out.println("id不存在，修改失败！");
            return;
        }
        Student stu = list.get(pos);

        System.out.println("请输入学生学号：");
        String newId= sc.next();
        stu.setId(newId);

        System.out.println("请输入学生姓名：");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请输入学生年龄：");
        int newAge = sc.nextInt();
        stu.setAge(newAge);

        System.out.println("请输入学生地址：");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        System.out.println("修改学生成功！");

    }

    //查询学生
    public static void searchStudent(ArrayList<Student> list){
        System.out.println("查询学生");
        if(list.size() == 0){
            System.out.println("当前无学生信息，请添加后再查询!");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+'\t'+stu.getName()+"\t\t"+stu.getAge()+"\t\t"+stu.getAddress());
        }
    }

    //id是否存在
    public static boolean contains(ArrayList<Student> list, String id){
        return index(list, id) >= 0;
    }

    //返回id的索引值
    public static int index(ArrayList<Student> list, String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sId = stu.getId();
            if(sId.equals(id)){
                return i;
            }
        }
        return -1;
    }
}
